package org.vectrics.recruiter.struts.admin;

import java.util.Calendar;

import org.vectrics.common.util.DateSync;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.InterviewCriteria;
import org.vectrics.user.PersonEncounterStatusType;


public class AdminInterviewSummaryTag extends AdminSummaryBaseTag {

	private static final long serialVersionUID = 5133721246063702940L;

	public AdminInterviewSummaryTag() {
		super();
	}
	
	protected String getHeader() {
		return CoreServiceLocator.getPhraseProvider().getMessage("heading.interview.summary", this.getLocale());
	}
	
	protected void fillContentCell() {
		this.showScheduledInterviews();
		this.showMyScheduledInterviews();
	}

	
	private void showScheduledInterviews() {
		Calendar nowMinus2Hours = Calendar.getInstance();
		nowMinus2Hours.setTime(DateSync.getCurrentTime());
		nowMinus2Hours.add(Calendar.HOUR, -2);

		Calendar nowPlus4Days = Calendar.getInstance();
		nowMinus2Hours.setTime(DateSync.getCurrentTime());
		nowMinus2Hours.add(Calendar.DATE, 4);
		
		InterviewCriteria interviewCriteria = new InterviewCriteria();
		interviewCriteria.addDomain(ThreadProperties.getDomain());
		interviewCriteria.addStatusCode(PersonEncounterStatusType.STATUS_SCHEDULED);
		interviewCriteria.setStartDate(nowMinus2Hours.getTime());
		interviewCriteria.setEndDate(nowPlus4Days.getTime());
		int numberOfScheduledInterviews = RecruitServiceLocator.getInterviewSearcher().findInterviewCount(interviewCriteria);
		this.addContentRow("" + numberOfScheduledInterviews + " interviews are scheduled for the next 4 days.");
		
	}
	
	private void showMyScheduledInterviews() {
		Calendar nowMinus2Hours = Calendar.getInstance();
		nowMinus2Hours.setTime(DateSync.getCurrentTime());
		nowMinus2Hours.add(Calendar.HOUR, -2);

		Calendar nowPlus4Days = Calendar.getInstance();
		nowMinus2Hours.setTime(DateSync.getCurrentTime());
		nowMinus2Hours.add(Calendar.DATE, 7);
		
		InterviewCriteria interviewCriteria = new InterviewCriteria();
		interviewCriteria.addDomain(ThreadProperties.getDomain());
		interviewCriteria.addStatusCode(PersonEncounterStatusType.STATUS_SCHEDULED);
		interviewCriteria.setStartDate(nowMinus2Hours.getTime());
		interviewCriteria.setEndDate(nowPlus4Days.getTime());
		interviewCriteria.setInterviewer(this.getAuthPerson());

		int numberOfScheduledInterviews = RecruitServiceLocator.getInterviewSearcher().findInterviewCount(interviewCriteria);
		this.addContentRow("" + numberOfScheduledInterviews + " interviews are scheduled where" +
				" you are the interviewer, for the next 7 days.",
				this.getContextPath() + "/admin/interview/myinterviews/index.do?personId=" + this.getAuthPerson().getId(),
				"view");
	}

}
